//
//  InTrainingItemModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/29.
//

import UIKit
import HandyJSON
import MYModule

open class TrainingItemModel: NSObject, HandyJSON {
    
    
    /// 现在的数据
    public var currentData: Double = 0
    
    /// 设置的目标数据
    public var targetData: Double?
    
    /// 主题
    public var title: String?
    
    /// 单位
    public var unit: String?
    
    /// 数据类型
    public var contentType: TrainingTypeContent = .unkown
    
    public required override init() {}
    
    public init(title: String) {
        super.init()
        
        self.title = title
       
    }
    
    public var highProgress: Double? {
        
       
        switch contentType {
        case .pace:
            return 0.5
        default:
            return nil
        }
    }
    
    public var progress: Double? {
        
        guard let targetData = targetData else { return nil }
        if targetData == 0 {
            return nil
        }
        
        switch contentType {
        case .pace:
            // 目标配速是进度条的一半，配速为0时，进度条在最左侧，配速在0-目标配速时，在中心点右侧；配速在目标配速-目标配速*2时，在中心点左侧
            if currentData <= 0 {
                return 0.0
            }
            if currentData >= targetData * 2 {
                return 0.0
            }
            
            if currentData >= targetData {
                return 0.5 - (currentData - targetData) / (targetData * 2)
            }
            if currentData < targetData {
                return 0.5 + (targetData - currentData ) / (targetData * 2)
            }
            
            return 0.0
        default:
            return currentData/targetData
        }
        
    }
    
    /// 数据 展示
    public var currentDataTitle: String {
        
        switch contentType {
            
        case .unkown:
            return "--"
            
        case .time:
            return timeOf(Int(currentData))
        case .step:
            return Int(currentData).normalTitle
        case .heartRate:
            return Int(currentData).normalTitle
        case .calorie:
            //TODO: 目前统一为0时显示--
            return  Int(currentData).normalTitle//"\(Int(currentData))"
        case .distance:
            //TODO: 目前统一为0时显示--
            return currentData.normalTitle//distanceOf(currentData)
        case .pace:
            return paceOf(currentData)
        }
    }
    
    /// 设置的目标展示
    public var targetDataTitle: String? {
        
        guard let targetData = targetData else {
            return nil
        }
        
        switch contentType {
            
        case .unkown:
            return ""
            
        case .time:
            return timeOf(Int(targetData))
        case .step:
            return Int(targetData).normalTitle
        case .heartRate:
            return Int(targetData).normalTitle
        case .calorie:
            //TODO: 目前统一为0时显示--
            return  Int(targetData).normalTitle//"\(Int(targetData))"
        case .distance:
            //TODO: 目前统一为0时显示--
            
            return targetData.normalTitle//distanceOf(targetData)
        case .pace:
            
            let pace = Unit.sharedInstance.goalMetricPace(targetData)
            return paceOf(pace)
        }
    }

    
    open func distanceOf(_ distance: Double) -> String {
        
        return distance.leadingZero
    }
    
    open func timeOf(_ seconds: Int) ->String {
        
        let tuple = Unit.sharedInstance.timeOf(seconds: seconds, granularity: .hour)
        let time = String(format: "%02d:%02d:%02d", tuple.hour, tuple.minute, tuple.second)
        return time
    }
    
    
    
    open func paceOf(_ paceMinute: Double) ->String {
        let m = Int(paceMinute)
        let s = Int(paceMinute * 60 - Double(m * 60))
        
        var pace = "\(m.leadingZero)'\(s.leadingZero)\""
        if m == 0 && s == 0 {
            //TODO: 目前统一为0时显示--
            pace = "--'--\""//"00'00\""
        }
        return pace
        
    }
    
    open func calorieOf(_ calorie: Int) ->String {
        
        return calorie.leadingZero
    }
    
}



